package com.ximu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.ximu.user.entity.LoginUser;
import com.ximu.user.entity.User;
import com.ximu.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @projectName: java
 * @package: com.ximu.service_ucenter.service.impl
 * @className: UserDetailsServiceImpl
 * @author: ximu
 * @description: 重写UserDetails
 * @date: 2022/5/14 13:42
 * @version: 1.0
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {



    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //查询用户信息
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,username);
        User user = userMapper.selectOne(queryWrapper);
        //如果查询为空就抛异常，会被security的异常处理捕获
        if (Objects.isNull(user)){
            throw new RuntimeException("没有查到相关用户");
        }



        return  new LoginUser(user);

    }
}
